package com.xxl.job.admin;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.solon.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.solon.plugins.inner.PaginationInnerInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.solon.annotation.Db;
import org.noear.snack.core.Feature;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.AppContext;
import org.noear.solon.serialization.snack3.SnackActionExecutor;
import org.noear.solon.serialization.snack3.SnackRenderFactory;

import javax.sql.DataSource;
import java.util.Date;

/**
 * 应用程序启动配置
 *
 * @author xieshuang
 * @since 2024/06/04
 */
@Configuration
public class ApplicationStartConfig {

    @Inject
    private AppContext appContext;

    @Bean(value = "master", typed = true)
    public DataSource master(@Inject("${db.master}") HikariDataSource ds) {
        return ds;
    }

    @Bean(name = "dateMetaObjectHandler")
    public MetaObjectHandler dateMetaObjectHandler() {
        return new MetaObjectHandler() {
            @Override
            public void insertFill(MetaObject metaObject) {
                this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
                this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
                this.strictInsertFill(metaObject, "createTime", Long.class, System.currentTimeMillis());
                this.strictInsertFill(metaObject, "updateTime", Long.class, System.currentTimeMillis());
                this.strictInsertFill(metaObject, "createDate", Date.class, new Date());
                this.strictInsertFill(metaObject, "updateDate", Date.class, new Date());
                this.strictInsertFill(metaObject, "createDate", Long.class, System.currentTimeMillis());
                this.strictInsertFill(metaObject, "updateDate", Long.class, System.currentTimeMillis());
            }

            @Override
            public void updateFill(MetaObject metaObject) {
                this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
                this.strictUpdateFill(metaObject, "updateTime", Long.class, System.currentTimeMillis());
                this.strictInsertFill(metaObject, "updateDate", Date.class, new Date());
                this.strictInsertFill(metaObject, "updateDate", Long.class, System.currentTimeMillis());
            }
        };
    }

    @Bean
    public void masterDbCfg(@Db("master") MybatisConfiguration cfg,
                            @Db("master") GlobalConfig globalConfig,
                            @Inject("dateMetaObjectHandler") MetaObjectHandler dateMetaObjectHandler) {
        cfg.setCacheEnabled(false);
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        cfg.addInterceptor(interceptor);
        globalConfig.setMetaObjectHandler(dateMetaObjectHandler);
    }

    @Bean
    public void jsonInit(@Inject SnackRenderFactory factory, @Inject SnackActionExecutor executor){
        factory.addFeatures(Feature.UseGetter);  //增加特性 //只使用 getter 属性做序列化输出
    }

}
